<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>login</title>
    <link rel="stylesheet" href="../css/data.css">
    <style>

    </style>
</head>
<body>

<form action="reg.php" method="post" name="loginForm" class="loginForm">
    <fieldset>
        <legend>注册</legend>
        <div class="itm">
            <input type="text" id="username" name="username" placeholder="用户名"
                   pattern="[A-Za-z0-9\-\u4e00-\u9fa5]{3,10}" title="至少三个字十个字以内的用户名"/>
        </div>
        <div class="itm">
            <input type="password" id="paw" name="pwd" placeholder="密码（至少5位数的密码)"
                   pattern="[A-Za-z0-9\-]{5,12}" title="至少5位最多12位的数字，字母组成，区分大小写"/>
        </div>
        <div class="itm">
            <input type="password" id="paw" name="pwd2" placeholder="确认密码"
                   pattern="[A-Za-z0-9\-]{5,12}" />
        </div>
        <div class="itm">
            <select name="city" >
                <option value="ChengDu" selected>默认城市：成都</option>
                <option value="BeiJing">北京</option>
                <option value="Paris">巴黎</option>
                <option value="Rome">罗马</option>
                <option value="NewYork">纽约</option>
            </select>
        </div>
        
        <div class="itm">
            <button type="submit" name="submit">注册</button>
            <a href="../index.php">登录</a>
        </div>
    </fieldset>


    <script>
        check();
        function check() {
            var form = document.loginForm;
            form.submit.onclick = function () {
                if (form.username.value === "" || form.pwd.value === "") {
                    alert("用户名或密码不能为空");
                    return false;
                }else if( form.pwd.value !== form.pwd2.value ){
                    alert("两次密码不一致");
                    return false;
                }
                else {
                    return true;
                }
            }
        }

    </script>
</body>
</html>




<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/29
 * Time: 16:47
 */

header("content-type:text/html;charset=utf-8");
if (!empty($_POST)) {
    reg();
}
function reg(){
    if ($_POST['username'] === "" || $_POST['pwd'] === "") {
        echo "<div style='color: red;margin-top: 20px;'>用户名或者密码不能为空</div>";
        exit();
    }
    if($_POST['pwd'] !== $_POST['pwd2']){
        echo "<div style='color: red;margin-top: 20px;'>两次密码不一致</div>";
        exit();
    }

    $link = mysqli_connect('localhost', 'root', 'root', "test");
    if (!$link) {
        exit("连接失败");
    }
    $user = $_POST['username'];
    $pwd = md5($_POST['pwd']);
    $city = $_POST['city'];
    $sql = "select * from user WHERE  username ='$user'";
    $re = mysqli_fetch_array(mysqli_query($link,$sql));

    if($re){
        echo "<div style='color: red;margin-top: 20px;'>此用户名已创建</div>";
    }else{
        $sql = "insert into user (username,pwd,city,add_time) VALUE ('$user','$pwd','$city',NOW())";
        mysqli_query($link,$sql);
        if(!mysqli_affected_rows($link)){
            exit("添加数据失败");
        }else{
            echo '<div style="margin-top: 20px">添加数据成功，请<a href="../index.php" style="color: red">登录</a>后执行操作</div>';
        }
    }
}
?>